﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using WCF;

namespace GeekMeet
{
    /// <summary>
    /// This is the master file that is used on every page, that holds headerinfo, logo, 
    /// menu and notification menu
    /// </summary>
    public partial class Site1 : System.Web.UI.MasterPage
    {
        GeekMeetService wcf = new GeekMeetService();
        bool IsLoggedIn = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;

        protected void Page_Load(object sender, EventArgs e)
        {
            SetMenuState();

            //If the user is logged in
            if (IsLoggedIn)
            {

                //Store Guid
                Guid loggedInUserGUID = (Guid)Membership.GetUser().ProviderUserKey;

                // Get new notifications on load
                GetNotifications(loggedInUserGUID);
                CheckPremium();
            
            }
        }

        /// <summary>
        /// Sets the menu depending on if user is logged in
        /// </summary>
        private void SetMenuState()
        {
            // Decide whether to show or hide user menu
            if (IsLoggedIn)
            {
                panMenu.Visible = true;
                panNotLoggedIn.Visible = false;
            }
            else
            {
                panMenu.Visible = false;
                panNotLoggedIn.Visible = true;
            }
        }

        /// <summary>
        /// Checks if the user is a premium user and enables part of the menu if not
        /// </summary>
        private void CheckPremium()
        {
            // Decide whether to disable messages in menu
            bool IsPremium = Roles.IsUserInRole("premiumuser");

            if (!IsPremium)
            {
                // User is not premium, so disable Messages and Premium menu items
                lnkMessages.Attributes.Add("onClick",
                    "return pleaseUpgrade('Den här funktionen är endast tillgänglig för Premium-användare.')");
                mnuMainMenu.Items[2].NavigateUrl = "javascript:pleaseUpgrade('Den här funktionen är endast tillgänglig för Premium-användare.')";
                mnuMainMenu.Items[5].NavigateUrl = "javascript:pleaseUpgrade('Den här funktionen är endast tillgänglig för Premium-användare.')";
                
            }
        }

        /// <summary>
        /// Checks if there is any new messages, friendrequests or wallposts
        /// </summary>
        /// <param name="loggedInUserGUID"></param>
        private void GetNotifications(Guid loggedInUserGUID)
        {
            //Calls a javascript function that checks the notifications
            Page.ClientScript.RegisterStartupScript(
                   this.GetType(), "getNotifications", "getNotifications('" + loggedInUserGUID + "');", true);
        }
    }
}